package interfaces.tables;

import java.util.ArrayList;
import java.util.List;

import logic.common.player.Player;

public class PlayerMap<T> {
	
	protected List<Player> players;
	protected List<T> values;
	
	public PlayerMap() {
		players = new ArrayList<Player>();
		values = new ArrayList<T>();
	}
	
	public void put(Player player, T value) {
		validateSizes();
		
		players.add(player);
		values.add(value);
		
		validateSizes();
	}
	
	public T remove(Player player){
		validateSizes();
		
		int index = players.indexOf(player);
		T value = null;
		if(index >= 0) {
			players.remove(index);
			value = values.get(index);
			values.remove(index);
		}
		
		validateSizes();
		return value;
	}
	
	public boolean containsKey(Player player) {
		for(Player p : players) {
			if(p == player) return true;
		}
		return false;
	}
	
	public T get(Player player) {
		int index = players.indexOf(player);
		if(index >= 0) return values.get(index);
		return null;
	}
	
	protected void validateSizes() {
		assert(players.size() == values.size());
	}
}